package marmalius;

import java.sql.*;

import marmalius.tools.DBService;
import marmalius.tools.LogService;

public class NPCHandler {
	private final static NPCHandler instance = new NPCHandler();
	
	private int jadMage = 0;
	//private client v;
	//private int hitHighTimer = 0;
	//private int hitTimer = 0;
	//private int hitProtectTimer = 0;
	public boolean hitDamage = false;
	private int maxNPCs = 5000;
	public static int maxListedNPCs = 5000;
	public static int maxNPCDrops = 10000;
	private NPC npcs[]; //les npcs spawn il me semble
	public NPCList NpcList[] = new NPCList[5000]; // maxListedNPCs npc_template ou un truc du genre
	public static NpcDrop npcdrop = null;
	
	public NPCHandler() {
		npcs = new NPC[maxNPCs];
		for (int i = 0; i < maxNPCs; i++)
			npcs[i] = null;
		for (int i = 0; i < maxListedNPCs; i++)
			NpcList[i] = null;
		npcdrop = new NpcDrop();
		loadNPCList();
		loadAutoSpawn();
		loadNPCDrops();
	}
	
	public void archerGfx2(int startID, int movingID, int casterY, int casterX, int offsetY, int offsetX, int index, int enemyY, int enemyX) {
		try {
			GraphicsHandler.createArrow(startID, casterY, casterX, offsetY, offsetX, 50, 95, movingID, 43, 21, enemyY, enemyX, index + 1);
		}
		catch (Exception E) {}
	}
	
	public boolean HitPlayer(int index) {
		if (PlayerHandler.getInstance().getPlayer(index) == null) {
			return false;
		}
		client c = (client)PlayerHandler.getInstance().getPlayer(index);
		int BonusUsed = c.CheckBestBonus();
		int enemyDef = PlayerHandler.getInstance().getPlayer(index).playerBonus[BonusUsed + 5] + (PlayerHandler.getInstance().getPlayer(index).playerLevel[1] / 4);
		double npcAtt = 0;
		int npcHits = npcs[index].HP;
		npcAtt += (double) (npcHits * 0.55);
		int npcAtt2 = ((int) Math.floor(npcAtt)) * 3;
		if (misc.random(npcAtt2) > misc.random(enemyDef)) {
			return true;
		}
		return false;
	}
	
	public boolean HitPlayerMage(int index) {
		if (PlayerHandler.getInstance().getPlayer(index) == null) {
			return false;
		}
		client c = (client)PlayerHandler.getInstance().getPlayer(index);
		int BonusUsed = c.CheckBestBonusMage();
		int enemyDef = PlayerHandler.getInstance().getPlayer(index).playerBonus[BonusUsed + 5] + (PlayerHandler.getInstance().getPlayer(index).playerLevel[6] / 4);
		double npcAtt = 0;
		int npcHits = npcs[index].HP;
		npcAtt += (double) (npcHits * 0.55);
		int npcAtt2 = ((int) Math.floor(npcAtt)) * 3;
		if (misc.random(npcAtt2) > misc.random(enemyDef)) {
			return true;
		}
		return false;
	}
	
	public void poisonNpc(int index) {
		npcs[index].poisonDmg = true;
		npcs[index].poisonDelay = misc.random(60);
		npcs[index].hitDiff = misc.random(5);
		npcs[index].hitDiff = npcs[index].hitDiff;
		npcs[index].updateRequired = true;
		npcs[index].hitUpdateRequired = true;
	}
	
	public boolean checkNpcId (int npcid) {
		for (int i = 0; i < maxNPCs; i++) {
			if (npcs[i] != null && npcs[i].npcId == npcid)
				return true;
		}
		return false;
	}
	
	public NPC getNPC(int index) { return npcs[index]; }
	public NPC[] getNPCS() { return npcs; }
	public static NPCHandler getInstance() { return instance; }
	public int getMaxNPC() { return maxNPCs; }

	public void newNPC(int npcType, int x, int y, int heightLevel, int rangex1, int rangey1, int rangex2, int rangey2, int WalkingType, int HP, boolean Respawns) {
		// first, search for a free slot
		int slot = -1;
		for (int i = 1; i < maxNPCs; i++) {
			if (npcs[i] == null) {
				slot = i;
				break;
			}
		}
		if (slot == -1)
			return;		// no free slot found
		if (HP <= 0) // This will cause client crashes if we don't use this :) - xero
			HP = 100;
		NPC newNPC = new NPC(slot, npcType);
		newNPC.absX = x;
		newNPC.absY = y;
		newNPC.makeX = x;
		newNPC.makeY = y;
		newNPC.moverangeX1 = rangex1;
		newNPC.moverangeY1 = rangey1;
		newNPC.moverangeX2 = rangex2;
		newNPC.moverangeY2 = rangey2;
		newNPC.walkingType = WalkingType;
		newNPC.HP = HP;
		newNPC.MaxHP = HP;
		newNPC.MaxHit = (int) Math.floor((HP / 10));
		if (newNPC.MaxHit < 1)
			newNPC.MaxHit = 1;
		newNPC.heightLevel = heightLevel;
		newNPC.Respawns = Respawns;
		npcs[slot] = newNPC;
		if (newNPC.MaxHP <= 0)
			newNPC.attackable = false;
	}

	public void hitDamage(int index, int hitDiff) {
		for (int i = 0; i < maxNPCs; i++) {
			int Player = npcs[i].StartKilling;
			hitDiff = PlayerHandler.getInstance().getPlayer(Player).hitDiff;
			PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
			PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
			PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
		}
	}

	public int petnpc(int npcType, int x, int y, int heightLevel, int rangex1, int rangey1, int rangex2, int rangey2, int WalkingType, int HP) {
		// first, search for a free slot
		int slot = -1;
		for (int i = 1; i < maxNPCs; i++) {
			if (npcs[i] == null) {
				slot = i;
				break;
			}
		}
		NPC newNPC = new NPC(slot, npcType);
		newNPC.absX = x;
		newNPC.absY = y;
		newNPC.makeX = x;
		newNPC.makeY = y;
		newNPC.moverangeX1 = rangex1;
		newNPC.moverangeY1 = rangey1;
		newNPC.moverangeX2 = rangex2;
		newNPC.moverangeY2 = rangey2;
		newNPC.walkingType = WalkingType;
		newNPC.HP = HP;
		newNPC.MaxHP = HP;
		newNPC.MaxHit = (int) Math.floor((HP / 10));
		if (newNPC.MaxHit < 1) {
			newNPC.MaxHit = 1;
		}
		newNPC.heightLevel = heightLevel;
		npcs[slot] = newNPC;
		return slot;
	}

	public void newNPCList(int npcType, String npcName, int combat, int HP) {
		// first, search for a free slot
		int slot = -1;
		for (int i = 0; i < maxListedNPCs; i++) {
			if (NpcList[i] == null) {
				slot = i;
				break;
			}
		}
		if (slot == -1) {
			return;
		}		// no free slot found
		NPCList newNPCList = new NPCList(npcType);
		newNPCList.npcName = npcName;
		newNPCList.npcCombat = combat;
		newNPCList.npcHealth = HP;
		NpcList[slot] = newNPCList;
	}
	
	public int GetMove(int Place1, int Place2) { // Thanks to diablo for this! Fixed my npc follow code <3
		if ((Place1 - Place2) == 0) {
			return 0;
		}
		else if ((Place1 - Place2) < 0) {
			return 1;
		}
		else if ((Place1 - Place2) > 0) {
			return -1;
		}
		return 0;
	}

	public void FollowPlayer(int NPCID) {
		int follow = npcs[NPCID].followPlayer;
		int playerX = PlayerHandler.getInstance().getPlayer(follow).absX;
		int playerY = PlayerHandler.getInstance().getPlayer(follow).absY;
		npcs[NPCID].RandomWalk = false;
		if (PlayerHandler.getInstance().getPlayer(follow) != null) {
			if (playerY < npcs[NPCID].absY) {
				npcs[NPCID].moveX = GetMove(npcs[NPCID].absX, playerX);
				npcs[NPCID].moveY = GetMove(npcs[NPCID].absY, playerY + 1);
			}
			else if (playerY > npcs[NPCID].absY) {
				npcs[NPCID].moveX = GetMove(npcs[NPCID].absX, playerX);
				npcs[NPCID].moveY = GetMove(npcs[NPCID].absY, playerY - 1);
			}
			else if (playerX < npcs[NPCID].absX) {
				npcs[NPCID].moveX = GetMove(npcs[NPCID].absX, playerX + 1);
				npcs[NPCID].moveY = GetMove(npcs[NPCID].absY, playerY);
			}
			else if (playerX > npcs[NPCID].absX) {
				npcs[NPCID].moveX = GetMove(npcs[NPCID].absX, playerX - 1);
				npcs[NPCID].moveY = GetMove(npcs[NPCID].absY, playerY);
			}
			else if (playerY < npcs[NPCID].absY + 1) {
				npcs[NPCID].moveX = GetMove(npcs[NPCID].absX, playerX);
				npcs[NPCID].moveY = GetMove(npcs[NPCID].absY, playerY + 2);
			}
			else if (playerY > npcs[NPCID].absY + 1) {
				npcs[NPCID].moveX = GetMove(npcs[NPCID].absX, playerX);
				npcs[NPCID].moveY = GetMove(npcs[NPCID].absY, playerY - 2);
			}
			else if (playerX < npcs[NPCID].absX + 1) {
				npcs[NPCID].moveX = GetMove(npcs[NPCID].absX, playerX + 2);
				npcs[NPCID].moveY = GetMove(npcs[NPCID].absY, playerY);
			}
			else if (playerX > npcs[NPCID].absX + 1) {
				npcs[NPCID].moveX = GetMove(npcs[NPCID].absX, playerX - 2);
				npcs[NPCID].moveY = GetMove(npcs[NPCID].absY, playerY);
			}
			else if (playerY < npcs[NPCID].absY - 1) {
				npcs[NPCID].moveX = GetMove(npcs[NPCID].absX, playerX);
				npcs[NPCID].moveY = GetMove(npcs[NPCID].absY, playerY + 2);
			}
			else if (playerY > npcs[NPCID].absY - 1) {
				npcs[NPCID].moveX = GetMove(npcs[NPCID].absX, playerX);
				npcs[NPCID].moveY = GetMove(npcs[NPCID].absY, playerY - 2);
			}
			else if (playerX < npcs[NPCID].absX - 1) {
				npcs[NPCID].moveX = GetMove(npcs[NPCID].absX, playerX + 2);
				npcs[NPCID].moveY = GetMove(npcs[NPCID].absY, playerY);
			}
			else if (playerX > npcs[NPCID].absX - 1) {
				npcs[NPCID].moveX = GetMove(npcs[NPCID].absX, playerX - 2);
				npcs[NPCID].moveY = GetMove(npcs[NPCID].absY, playerY);
			}
			npcs[NPCID].getNextNPCMovement();
			npcs[NPCID].updateRequired = true;
		}
	}

	public void FollowPlayerCB(int NPCID, int playerID) {
		int playerX = PlayerHandler.getInstance().getPlayer(playerID).absX;
		int playerY = PlayerHandler.getInstance().getPlayer(playerID).absY;
		npcs[NPCID].RandomWalk = true;
		if (PlayerHandler.getInstance().getPlayer(playerID) != null) {
			if (playerY < npcs[NPCID].absY) {
				npcs[NPCID].moveX = GetMove(npcs[NPCID].absX, playerX);
				npcs[NPCID].moveY = GetMove(npcs[NPCID].absY, playerY + 1);
			}
			else if (playerY > npcs[NPCID].absY) {
				npcs[NPCID].moveX = GetMove(npcs[NPCID].absX, playerX);
				npcs[NPCID].moveY = GetMove(npcs[NPCID].absY, playerY - 1);
			}
			else if (playerX < npcs[NPCID].absX) {
				npcs[NPCID].moveX = GetMove(npcs[NPCID].absX, playerX + 1);
				npcs[NPCID].moveY = GetMove(npcs[NPCID].absY, playerY);
			}
			else if (playerX > npcs[NPCID].absX) {
				npcs[NPCID].moveX = GetMove(npcs[NPCID].absX, playerX - 1);
				npcs[NPCID].moveY = GetMove(npcs[NPCID].absY, playerY);
			}											 
			npcs[NPCID].updateRequired = true;
		}
	}

	public static boolean IsDropping = false;
	
	public void MonsterDropItem(int npcId) {
		int id = npcs[npcId].npcType;
		int play = GetNpcKiller(npcId);
		if (npcs[npcId] != null && PlayerHandler.getInstance().getPlayer(play) != null) {
			for (int a = 0; a <= npcdrop.maxFindDrop(id); a++) {
				if (Math.random() * 100 <= npcdrop.rHopeDrop(id, a)) {
					ItemHandler.addItem(npcdrop.rItem(id, a), npcs[npcId].absX, npcs[npcId].absY, (int)Math.round((Math.random() * npcdrop.rNbItem(id, a))), play, false);
				}
			}
		}
		/*
		if (IsDropping == false && 1 == 2) {
			IsDropping = true;
			int Play = GetNpcKiller(NPCID);
			int O = GetNpcKiller(NPCID);
			client C = (client) PlayerHandler.getInstance().players[O];
			int Maxi = server.itemHandler.DropItemCount;
			for (int i = 0; i <= Maxi; i++) {
				if (server.itemHandler.DroppedItemsID[i] > 0) {} else {
					int NPCID2 = NPCID + 34;
					System.out.println("Npc id =" + NPCID); 
					if (npcs[NPCID] != null && PlayerHandler.getInstance().players[Play] != null) {
						if (npcs[NPCID].npcType == 1615) {
							ItemHandler.addItem(Item2.randomAbyssal(), npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
							ItemHandler.addItem(592, npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
							C.addSkillXP(500, 18);
						}
						if (npcs[NPCID].npcType == 62) {
							ItemHandler.addItem(Item2.randomJungleSpider(), npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
						}
						if (npcs[NPCID].npcType == 41) {
							ItemHandler.addItem(314, npcs[NPCID].absX, npcs[NPCID].absY, misc.random(15), GetNpcKiller(NPCID), false);
						}
						if (npcs[NPCID].npcType == C.SlayerNPC) {
							C.SlayNPC();
						}
						if (npcs[NPCID].npcType == 912) {
							server.ZammyScore += 1;
							C.helpedTeam += 1;
							if (C.freeSlots() > 0) {
								C.addItem(6529, 30); 
							}		 
							if (C.freeSlots() < 1) {
								ItemHandler.addItem(6529, npcs[NPCID].absX, npcs[NPCID].absY, 30, GetNpcKiller(NPCID), false);
							}
						}
						if (npcs[NPCID].npcType == 913) {
							server.SaraScore += 1;
							C.helpedTeam += 1;
							if (C.freeSlots() > 0) {
								C.addItem(6529, 30); 
							}		 
							if (C.freeSlots() < 1) {
								ItemHandler.addItem(6529, npcs[NPCID].absX, npcs[NPCID].absY, 30, GetNpcKiller(NPCID), false);
							}
						}
						if (npcs[NPCID].npcType == 911) {
							server.SaraScore += 5;
							server.GuthixScore += 5;
							server.ZammyScore += 5;
							C.helpedTeam += 1;
							if (C.freeSlots() > 0) {
								C.addItem(6529, 100); 
							}		 
							if (C.freeSlots() < 1) {
								ItemHandler.addItem(6529, npcs[NPCID].absX, npcs[NPCID].absY, 100, GetNpcKiller(NPCID), false);
							}
						}
						if (npcs[NPCID].npcType == 914) {
							server.GuthixScore += 1;				 
							C.helpedTeam += 1;
							if (C.freeSlots() > 0) {
								C.addItem(6529, 30); 
							}		 
							if (C.freeSlots() < 1) {
								ItemHandler.addItem(6529, npcs[NPCID].absX, npcs[NPCID].absY, 30, GetNpcKiller(NPCID), false);
							}
						}
						if (npcs[NPCID].npcType == 1592) {
							ItemHandler.addItem(Item3.randomsteel(), npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
							ItemHandler.addItem(536, npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
							ItemHandler.addItem(2353, npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
							ItemHandler.addItem(2353, npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
							ItemHandler.addItem(2353, npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
							ItemHandler.addItem(2353, npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
						}
						if (npcs[NPCID].npcType == 2745) {
							ItemHandler.addItem(Item2.randomJad(), npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
						}
						if (npcs[NPCID].npcType == 1) {
							ItemHandler.addItem(Item2.randomMan(),
									npcs[NPCID].absX, npcs[NPCID].absY, 1,
									GetNpcKiller(NPCID), false);
							ItemHandler.addItem(592, npcs[NPCID].absX,
									npcs[NPCID].absY, 1, GetNpcKiller(NPCID),
									false);
							 ItemHandler.addItem(995, npcs[NPCID].absX,
									npcs[NPCID].absY, misc.random(90), GetNpcKiller(NPCID),
									false);
						}
						if (npcs[NPCID].npcType == 2881) {
							ItemHandler.addItem(Item2.randomRex(),
									npcs[NPCID].absX, npcs[NPCID].absY, 1,
									GetNpcKiller(NPCID), false);
							ItemHandler.addItem(536, npcs[NPCID].absX,
									npcs[NPCID].absY, 1, GetNpcKiller(NPCID),
									false);
						}
						if (npcs[NPCID].npcType == 2595) {
							ItemHandler.addItem(Item2.randomObbyT(),
									npcs[NPCID].absX, npcs[NPCID].absY, 1,
									GetNpcKiller(NPCID), false);
						}
						if (npcs[NPCID].npcType == 83) {
							ItemHandler.addItem(Item3.randomGreater(),
								npcs[NPCID].absX, npcs[NPCID].absY, 1,
									GetNpcKiller(NPCID), false);
							ItemHandler.addItem(532, npcs[NPCID].absX,
									npcs[NPCID].absY, 1, GetNpcKiller(NPCID),
									false);
						}
						if (npcs[NPCID].npcType == 50) {
							ItemHandler.addItem(Item3.randomKbd(),
									npcs[NPCID].absX, npcs[NPCID].absY, 1,
									GetNpcKiller(NPCID), false);
							ItemHandler.addItem(536, npcs[NPCID].absX,
									npcs[NPCID].absY, 1, GetNpcKiller(NPCID),
									false);
						}
						if (npcs[NPCID].npcType == 1643) {
							ItemHandler.addItem(Item2.randomMage(),
									npcs[NPCID].absX, npcs[NPCID].absY, 1,
									GetNpcKiller(NPCID), false);
							ItemHandler.addItem(592, npcs[NPCID].absX,
									npcs[NPCID].absY, 1, GetNpcKiller(NPCID),
									false);
						}
						if (npcs[NPCID].npcType == 158) {
							ItemHandler.addItem(Item3.randomshadow(),
									npcs[NPCID].absX, npcs[NPCID].absY, 1,
									GetNpcKiller(NPCID), false);
							ItemHandler.addItem(536, npcs[NPCID].absX,
									npcs[NPCID].absY, 1, GetNpcKiller(NPCID),
									false);
						}
						if (npcs[NPCID].npcType == 2060) {
							ItemHandler.addItem(Item2.randomslash(),
									npcs[NPCID].absX, npcs[NPCID].absY, 1,
									GetNpcKiller(NPCID), false);
							ItemHandler.addItem(592, npcs[NPCID].absX,
									npcs[NPCID].absY, 1, GetNpcKiller(NPCID),
									false);
						}
						if (npcs[NPCID].npcType == 2882) {
							ItemHandler.addItem(Item2.randomRex(),
									npcs[NPCID].absX, npcs[NPCID].absY, 1,
									GetNpcKiller(NPCID), false);
							ItemHandler.addItem(536, npcs[NPCID].absX,
									npcs[NPCID].absY, 1, GetNpcKiller(NPCID),
									false);
						}
						if (npcs[NPCID].npcType == 2887) {
							ItemHandler.addItem(Item2.randomDag(),
									npcs[NPCID].absX, npcs[NPCID].absY, 1,
									GetNpcKiller(NPCID), false);
							ItemHandler.addItem(532, npcs[NPCID].absX,
									npcs[NPCID].absY, 1, GetNpcKiller(NPCID),
									false);
						}
						if (npcs[NPCID].npcType == 2883) {
							ItemHandler.addItem(Item2.randomRex(), npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
							ItemHandler.addItem(536, npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
						}
						if (npcs[NPCID].npcType == 1160) {
							ItemHandler.addItem(Item2.randomKQ(), npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
							ItemHandler.addItem(536, npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
						}
						if (npcs[NPCID].npcType == 32) {
							ItemHandler.addItem(Item2.randomGGuard(), npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
							ItemHandler.addItem(526, npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
							ItemHandler.addItem(995, npcs[NPCID].absX, npcs[NPCID].absY, misc.random(90), GetNpcKiller(NPCID), false);
						}
						if (npcs[NPCID].npcType == 2015) {
							ItemHandler.addItem(Item2.randomMummy(), npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
							ItemHandler.addItem(592, npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
						}
						if (npcs[NPCID].npcType == 53) {
							ItemHandler.addItem(Item3.randomRedDragon(), npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
							ItemHandler.addItem(536, npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
						}
						if (npcs[NPCID].npcType == 3200) {
							ItemHandler.addItem(Item2.randomchaose(), npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
							ItemHandler.addItem(536, npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
						}
						if (npcs[NPCID].npcType == 2025) {
							C.killCount += 1;
						}
						if (npcs[NPCID].npcType == 2026) {
							C.killCount += 1;
						}
						if (npcs[NPCID].npcType == 2027) {
							C.killCount += 1;
						}
						if (npcs[NPCID].npcType == 2028) {
							C.killCount += 1;
						}
						if (npcs[NPCID].npcType == 2029) {
							C.killCount += 1;
						}
						if (npcs[NPCID].npcType == 2030) {
							C.killCount += 1;
						}
						if (npcs[NPCID].npcType == 9) {
							ItemHandler.addItem(Item2.randomGaurd(), npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false);
							ItemHandler.addItem(995, npcs[NPCID].absX, npcs[NPCID].absY, misc.random(90), GetNpcKiller(NPCID), false);
							ItemHandler.addItem(526, npcs[NPCID].absX, npcs[NPCID].absY, misc.random(90), GetNpcKiller(NPCID), false);
						}
					}
					if (i == Maxi) {
						if (server.itemHandler.DropItemCount >= (server.itemHandler.MaxDropItems + 1)) {
							server.itemHandler.DropItemCount = 0;
							println("! Notify item resterting !");
						}
					}
					break;
				}
			}
			IsDropping = false;
		}
		*/
	}

	public boolean IsInWorldMap(int coordX, int coordY) {
		for (int i = 0; i < worldmap[0].length; i++)
			return true;
		return false;
	}

	public boolean IsInWorldMap2(int coordX, int coordY) {
		for (int i = 0; i < worldmap2[0].length; i++) {
			if (worldmap2[0][i] == coordX && worldmap2[1][i] == coordY) {
				return true;
			}
		}
		return false;
	}

	public boolean IsInRange(int NPCID, int MoveX, int MoveY) {
		int NewMoveX = (npcs[NPCID].absX + MoveX);
		int NewMoveY = (npcs[NPCID].absY + MoveY);
		if (NewMoveX <= npcs[NPCID].moverangeX1 && NewMoveX >= npcs[NPCID].moverangeX2 && NewMoveY <= npcs[NPCID].moverangeY1 && NewMoveY >= npcs[NPCID].moverangeY2) {
			if ((npcs[NPCID].walkingType == 1 && IsInWorldMap(NewMoveX, NewMoveY) == true) || (npcs[NPCID].walkingType == 2 && IsInWorldMap2(NewMoveX, NewMoveY) == false)) {
				if (MoveX == MoveY) {
					if ((IsInWorldMap(NewMoveX, npcs[NPCID].absY) == true && IsInWorldMap(npcs[NPCID].absX, NewMoveY) == true) || (IsInWorldMap2(NewMoveX, npcs[NPCID].absY) == false && IsInWorldMap2(npcs[NPCID].absX, NewMoveY) == false)) {
						return true;
					}
					return false;
				}
				return true;
			}
		}
		return false;
	}

	public void PoisonNPC(int NPCID) {
		npcs[NPCID].PoisonClear = 0;
		npcs[NPCID].PoisonDelay = 40;
	}

	public void Poison(int NPCID) {
		if (npcs[NPCID].PoisonDelay <= 1) {
			int hitDiff = 3 + misc.random(15);
			npcs[NPCID].poisondmg = true;
			npcs[NPCID].hitDiff = hitDiff;
			npcs[NPCID].updateRequired = true;
			npcs[NPCID].hitUpdateRequired = true;
			npcs[NPCID].PoisonClear++;
			npcs[NPCID].PoisonDelay = 40;
		}
	}

	public void process() {
		for (int i = 0; i < maxNPCs; i++) {
			if (npcs[i] == null)
				continue;
			npcs[i].clearUpdateFlags();
		}
		for (int i = 0; i < maxNPCs; i++) {
			if (npcs[i] != null) {
				if (npcs[i].actionTimer > 0) {
					npcs[i].actionTimer--;
				}
				Poison(i);
				npcs[i].PoisonDelay -= 1;
				if (npcs[i].PoisonClear >= 15) {
					npcs[i].PoisonDelay = 9999999;
				}
				if (npcs[i].IsDead == false) {
					if (npcs[i].npcType == 1268 || npcs[i].npcType == 1266) {
						for (int j = 1; j < PlayerHandler.getInstance().getMaxPlayers(); j++) {
							if (PlayerHandler.getInstance().getPlayer(j) != null) {
								if (GoodDistance(npcs[i].absX, npcs[i].absY, PlayerHandler.getInstance().getPlayer(j).absX, PlayerHandler.getInstance().getPlayer(j).absY, 2) == true && npcs[i].IsClose == false) {
									npcs[i].actionTimer = 20;
									npcs[i].IsClose = true;
								}
							}
						}
						if (npcs[i].actionTimer == 0 && npcs[i].IsClose == true) {
							for (int j = 1; j < PlayerHandler.getInstance().getMaxPlayers(); j++) {
								if (PlayerHandler.getInstance().getPlayer(j) != null) {
									PlayerHandler.getInstance().getPlayer(j).RebuildNPCList = true;
								}
							}
							if (npcs[i].Respawns) {
								int old1 = (npcs[i].npcType - 1);
								int old2 = npcs[i].makeX;
								int old3 = npcs[i].makeY;
								int old4 = npcs[i].heightLevel;
								int old5 = npcs[i].moverangeX1;
								int old6 = npcs[i].moverangeY1;
								int old7 = npcs[i].moverangeX2;
								int old8 = npcs[i].moverangeY2;
								int old9 = npcs[i].walkingType;
								int old10 = npcs[i].MaxHP;
								npcs[i] = null;
								newNPC(old1, old2, old3, old4, old5, old6, old7, old8, old9, old10, true);
							}
						}
					}
					else if (npcs[i].RandomWalk == true && misc.random2(10) == 1 && npcs[i].moverangeX1 > 0 && npcs[i].moverangeY1 > 0 && npcs[i].moverangeX2 > 0 && npcs[i].moverangeY2 > 0) { // Move NPC
						int MoveX = misc.random(1);
						int MoveY = misc.random(1);
						int Rnd = misc.random2(4);
						if (Rnd == 1) {
							MoveX = -(MoveX);
							MoveY = -(MoveY);
						}
						else if (Rnd == 2) {
							MoveX = -(MoveX);
						}
						else if (Rnd == 3) {
							MoveY = -(MoveY);
						}
						if (IsInRange(i, MoveX, MoveY) == true) {
							npcs[i].moveX = MoveX;
							npcs[i].moveY = MoveY;
						}
						npcs[i].updateRequired = true;
					}
					else if (npcs[i].RandomWalk == false && npcs[i].IsUnderAttack == true) {
						if (npcs[i].npcType == 277 || npcs[i].npcType == 53 || npcs[i].npcType == 2361 || npcs[i].npcType == 3200 || npcs[i].npcType == 1643 || npcs[i].npcType == 2060 || npcs[i].npcType == 158
							|| npcs[i].npcType == 912 || npcs[i].npcType == 913 || npcs[i].npcType == 914 || npcs[i].npcType == 509 || npcs[i].npcType == 2745 || npcs[i].npcType == 2025 || npcs[i].npcType == 1592 || npcs[i].npcType == 2028) {
							AttackPlayerMage(i);
						}
						else {
							AttackPlayer(i);
						}
					}
					else if (npcs[i].followingPlayer && npcs[i].followPlayer > 0 && PlayerHandler.getInstance().getPlayer(npcs[i].followPlayer) != null) {
						if (PlayerHandler.getInstance().getPlayer(npcs[i].followPlayer).AttackingOn > 0) {
							int follow = npcs[i].followPlayer;
							npcs[i].StartKilling = PlayerHandler.getInstance().getPlayer(follow).AttackingOn;
							npcs[i].RandomWalk = true;
							npcs[i].IsUnderAttack = true;
							if (npcs[i].StartKilling > 0) {
								if (npcs[i].npcType == 277 || npcs[i].npcType == 53 || npcs[i].npcType == 2361 || npcs[i].npcType == 158 || npcs[i].npcType == 1643 || npcs[i].npcType == 2060
									|| npcs[i].npcType == 509 || npcs[i].npcType == 912 || npcs[i].npcType == 913 || npcs[i].npcType == 914 || npcs[i].npcType == 2745 || npcs[i].npcType == 2025 || npcs[i].npcType == 1592 || npcs[i].npcType == 2028) {
									AttackPlayerMage(i);
								}
								else {
									AttackPlayer(i);
								} 
							}
						}
						else {
							FollowPlayer(i);
						}
					}
					else if (npcs[i].followingPlayer && npcs[i].followPlayer > 0 && PlayerHandler.getInstance().getPlayer(npcs[i].followPlayer) != null) {
						if (PlayerHandler.getInstance().getPlayer(npcs[i].followPlayer).attacknpc > 0) {
							int follow = npcs[i].followPlayer;
							npcs[i].attacknpc = PlayerHandler.getInstance().getPlayer(follow).attacknpc;
							npcs[i].IsUnderAttackNpc = true;
							npcs[npcs[i].attacknpc].IsUnderAttackNpc = true;
							if (npcs[i].attacknpc > 0) {
								if (npcs[i].npcType == 1645 || npcs[i].npcType == 53 || npcs[i].npcType == 509 || npcs[i].npcType == 1241 || npcs[i].npcType == 1246) {
									AttackNPCMage(i);
								}
								else {												
									AttackNPC(i);
								}
							}
						}
						else {
							FollowPlayer(i);
						}
					}
					else if (npcs[i].IsUnderAttackNpc == true) {
						AttackNPC(i);
					}
					
					if (npcs[i].freezeTimer > 0) {
						npcs[i].freezeTimer--;
					}
					if (npcs[i].reducedAttack > 0) {
						npcs[i].reducedAttack--;
					}
					if (npcs[i].reducedAttack == 0) {
						npcs[i].MaxHit = (int) Math.floor((npcs[i].MaxHP / 10));
					}
					if (npcs[i].poisonTimer > 0) {
						npcs[i].poisonTimer--;
					}
					if (npcs[i].poisonTimer == 0) {
						npcs[i].poisonDmg = false;
						npcs[i].poisonDelay = -1;
					}
					if (npcs[i].HitTimer > 0) {
						npcs[i].HitTimer--;
					}
					if (npcs[i].poisonTimer > 0 && npcs[i].poisonDelay == 0) {
						poisonNpc(i);
					}
					if (npcs[i].reducedAttack > 0) {
						npcs[i].reducedAttack--;
					}
					if (npcs[i].npcType == 81 || npcs[i].npcType == 397
							|| npcs[i].npcType == 1766
							|| npcs[i].npcType == 1767
							|| npcs[i].npcType == 1768) {
						if (misc.random2(50) == 1) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "Moo";
						}
					}
					if (npcs[i].npcType == 1207) {				
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "Mage bank PKing Here!";
						}
					}
					if (npcs[i].npcType == 524) {				
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "Mining Here! I sell pickaxes also!";
						}
					}
					if (npcs[i].npcType == 1395) {				
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "Woodcutting center here!";
						}
					}
					if (npcs[i].npcType == 570) {				
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "Exchange your tokkul here!";
						}
					}
					if (npcs[i].npcType == 1663) {				
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "Agility Area Here!";
						}
					}
					if (npcs[i].npcType == 2100) {				
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "Main portal room here!";
						}
					}
					if (npcs[i].npcType == 1982) {				
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "Next game starts in "
									+ server.getInstance().GameTimer / 2 + " seconds!";
						}
					}
					if (npcs[i].npcType == 364) {				
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "Talk to me for your slayer task!";
						}
					}
					if (npcs[i].npcType == 2909) {				
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "Talk to me to check your points!";
						}
					}
					if (npcs[i].npcType == 693) {				
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "GodSword MiniGame Here!";
						}
					}
					if (npcs[i].npcType == 2107) {				
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "KQ Area Here!";
						}
					}
					if (npcs[i].npcType == 562) {				
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "Get Your Home Teleports Here! 6.3M Each!";
						}
					}
					if (npcs[i].npcType == 2234) {
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "Jad Area Here!";
						}
					}
					if (npcs[i].npcType == 3372) {
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "Red Dragon Area Here!";
						}
					}
					if (npcs[i].npcType == 198) {
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "Slayer Area Here!";
						}
					}
					if (npcs[i].npcType == 1281) {
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "KBD Area Here!";
						}
					}
					if (npcs[i].npcType == 2643) {					
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "Runecrafting Here!";
						}
					}
					if (npcs[i].npcType == 2288) {					
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "Range Training Area Here!";
						}
					}
					if (npcs[i].npcType == 2691) {					
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "Barrows Area Here!";
						}
					}
					if (npcs[i].RandomWalk == true) {
						npcs[i].getNextNPCMovement();
					}
					if (npcs[i].npcType == 2258) {					
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "Infernal Mage Area Here!";
						}
					}
					if (npcs[i].npcType == 220) {					
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "Fishing Area Here!";
						}
					}
					if (npcs[i].npcType == 2101) {					
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "Hangout/Trade Area Here!";
						}
					}
					if (npcs[i].npcType == 336) {					
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "The Chaos Elemental Area Here!";
						}
					}
					if (npcs[i].npcType == 29) {					
						if (misc.random2(30) <= 3) {
							npcs[i].updateRequired = true;
							npcs[i].textUpdateRequired = true;
							npcs[i].textUpdate = "Main Training Area Here!";
						}
					}
				}
				else if (npcs[i].IsDead == true) {
					if (npcs[i].actionTimer == 0 && npcs[i].DeadApply == false
							&& npcs[i].NeedRespawn == false) {
						if (server.NpcAnimHandler.die[npcs[i].npcType] != 0) {
							npcs[i].animNumber = server.NpcAnimHandler.die[npcs[i].npcType]; // npcanimhandler death emote
						}
						else {
							npcs[i].animNumber = server.NpcAnimHandler.die[npcs[i].npcType]; // npcanimhandler death emote
						}
						npcs[i].updateRequired = true;
						npcs[i].animUpdateRequired = true;
						npcs[i].DeadApply = true;
						npcs[i].actionTimer = 7;
					}
					else if (npcs[i].actionTimer == 0 && npcs[i].DeadApply == true && npcs[i].NeedRespawn == false) {
						MonsterDropItem(i);
						npcs[i].NeedRespawn = true;
						npcs[i].actionTimer = 20;
						npcs[i].absX = npcs[i].makeX;
						npcs[i].absY = npcs[i].makeY;
						npcs[i].animNumber = 0x328;
						npcs[i].HP = npcs[i].MaxHP;
						npcs[i].updateRequired = true;
						npcs[i].animUpdateRequired = true;
					}
					else if (npcs[i].actionTimer == 0 && npcs[i].NeedRespawn == true) {
						for (int j = 1; j < PlayerHandler.getInstance().getMaxPlayers(); j++) {
							if (PlayerHandler.getInstance().getPlayer(j) != null) {// PlayerHandler.getInstance().getPlayer(j).RebuildNPCList = true;
							}
						}
						if (npcs[i].Respawns) {
							int old1 = npcs[i].npcType;
							if (old1 == 1267 || old1 == 1265) {
								old1 += 1;
							}
							int old2 = npcs[i].makeX;
							int old3 = npcs[i].makeY;
							int old4 = npcs[i].heightLevel;
							int old5 = npcs[i].moverangeX1;
							int old6 = npcs[i].moverangeY1;
							int old7 = npcs[i].moverangeX2;
							int old8 = npcs[i].moverangeY2;
							int old9 = npcs[i].walkingType;
							int old10 = npcs[i].MaxHP;
							npcs[i] = null;
							newNPC(old1, old2, old3, old4, old5, old6, old7, old8, old9, old10, true);
						}
					}
				}
			}
		}
	}	

	public int random(int num) {
		return (int) (Math.random() * num);
	}
	
	public int GetNpcKiller(int NPCID) {
		int Killer = 0;
		int Count = 0;
		for (int i = 1; i < PlayerHandler.getInstance().getMaxPlayers(); i++) {
			if (Killer == 0) {
				Killer = i;
				Count = 1;
			}
			else {
				if (npcs[NPCID].Killing[i] > npcs[NPCID].Killing[Killer]) {
					Killer = i;
					Count = 1;
				}
				else if (npcs[NPCID].Killing[i] == npcs[NPCID].Killing[Killer]) {
					Count++;
				}
			}
		}
		if (Count > 1 && npcs[NPCID].Killing[npcs[NPCID].StartKilling] == npcs[NPCID].Killing[Killer]) {
			Killer = npcs[NPCID].StartKilling;
		}
		return Killer;
	}

	/*
	 WORLDMAP: (walk able places)
	 01 - Aubury
	 02 - Varrock Mugger
	 03 - Lowe
	 04 - Horvik
	 05 - Varrock General Store
	 06 - Thessalia
	 07 - Varrock Sword Shop
	 08 - Varrock East Exit Guards
	 09 - Falador General Store
	 10 - Falador Shield Shop
	 11 - Falador Mace Shop
	 12 - Falador Center Guards
	 13 - Falador North Exit Guards
	 14 - Barbarian Village Helmet Shop
	 15 - Varrock Staff Shop
	 16 - Al-Kharid Skirt Shop
	 17 - Al-Kharid Crafting Shop
	 18 - Al-Kharid General Store
	 19 - Al-Kharid Leg Shop
	 20 - Al-Kharid Scimitar Shop
	 21 - Lumbridge Axe Shop
	 22 - Lumbridge General Store
	 23 - Port Sarim Battleaxe Shop
	 24 - Port Sarim Magic Shop
	 25 - Port Sarim Jewelery Shop
	 26 - Port Sarim Fishing Shop
	 27 - Port Sarim Food Shop
	 28 - Rimmington Crafting Shop
	 29 - Rommington Archery Shop
	 30 - Npc's around and in varrock
	 31 - Npc's at Rellekka
	 32 - Npc's around and in lumbridge
	 33 - 
	 34 - 
	 35 - 
	 36 - 
	 37 - 
	 38 - 
	 39 - 
	 40 - 
	 */
	public static int worldmap[][] = {
		{
			 /* 01 */		3252, 3453, 3252, 3453, 3252, 3253, 3254, 3252, 3253,
			3254, 3255, 3252, 3253, 3252, 3253, /* 02 */3248, 3249, 3250, 3251,
			3252, 3253, 3254, 3248, 3249, 3250, 3251, 3252, 3253, 3254, 3248,
			3249, 3250, 3251, 3252, 3254, 3248, 3249, 3250, 3251, 3252, 3253,
			3254, 3248, 3249, 3250, 3251, 3252, 3253, 3254, 3248, 3249, 3250,
			3251, 3252, 3253, 3254, 3248, 3249, 3250, 3251, 3252, 3254, 3248,
			3249, 3250, 3251, 3252, 3253, 3254, 3248, 3249, 3250, 3251, 3252,
			3253, 3254, 3248, 3249, 3250, 3251, 3252, 3253, 3254, 3248, 3249,
			3250, 3251, 3252, 3253, 3254, 3248, 3249, 3250, 3251, 3252, 3253,
			3254, 3248, 3249, 3250, 3251, 3252, 3253, 3254, /* 03 */3235, 3234,
			3233, 3232, 3231, 3230, 3235, 3230, 3235, 3234, 3233, 3232, 3231,
			3230, 3234, 3232, 3231, 3234, 3233, 3232, 3231, 3234, 3233, 3232,
			3233, 3231, /* 04 */3231, 3230, 3229, 3232, 3231, 3230, 3229, 3229,
			3228, 3227, 3229, 3227, 3232, 3231, 3230, 3229, 3228, 3227, 3232,
			3231, 3230, 3229, 3228, 3227, 3226, 3225, 3232, 3231, 3230, 3229,
			3228, 3227, 3225, 3232, 3231, 3230, 3229, 3228, 3227, 3225, 3232,
			3229, 3228, 3227, 3226, 3232, 3231, 3230, 3229, /* 05 */3217, 3216,
			3215, 3214, 3219, 3218, 3217, 3216, 3219, 3218, 3217, 3219, 3217,
			3216, 3215, 3219, 3218, 3217, 3216, 3215, 3214, 3220, 3219, 3217,
			3216, 3215, 3214, 3219, 3217, 3216, 3215, 3214, 3219, 3217, 3216,
			3215, 3214, 3218, 3217, /* 06 */3207, 3206, 3205, 3208, 3207, 3206,
			3203, 3207, 3206, 3205, 3204, 3203, 3207, 3206, 3205, 3204, 3203,
			3202, 3208, 3207, 3206, 3205, 3208, 3207, 3206, 3207, /* 07 */3206,
			3204, 3203, 3202, 3209, 3208, 3207, 3205, 3203, 3208, 3207, 3206,
			3205, 3203, 3208, 3207, 3206, 3205, 3204, 3203, 3202, 3208, 3207,
			3206, 3205, 3203, 3207, 3206, 3203, 3206, 3203, 3206, 3205, 3205,
			3205, /* 08 */3268, 3268, 3268, 3268, 3268, 3269, 3269, 3269, 3269,
			3269, 3270, 3270, 3270, 3270, 3270, 3271, 3271, 3271, 3271, 3271,
			3272, 3272, 3272, 3272, 3272, 3273, 3273, 3273, 3273, 3273, 3274,
			3274, 3274, 3274, 3274, 3275, 3275, 3275, 3276, 3276, 3276, 3276,
			3273, 3274, 3275, 3276, 3273, 3274, 3275, 3273, /* 09 */2958, 2957,
			2959, 2958, 2957, 2959, 2958, 2957, 2959, 2958, 2957, 2956, 2955,
			2954, 2953, 2960, 2959, 2956, 2955, 2953, 2960, 2959, 2957, 2956,
			2953, /* 10 */2979, 2977, 2976, 2975, 2974, 2973, 2972, 2979, 2978,
			2977, 2972, 2972, 2974, 2973, 2972, /* 11 */2952, 2950, 2949, 2948,
			2952, 2951, 2950, 2949, 2948, 2952, 2951, 2950, 2949, 2948, 2952,
			2951, 2950, 2949, 2948, 2952, 2952, 2951, /* 12 */2969, 2967, 2966,
			2965, 2964, 2963, 2962, 2961, 2960, 2959, 2958, 2969, 2968, 2967,
			2966, 2965, 2964, 2963, 2962, 2961, 2960, 2959, 2958, 2969, 2968,
			2967, 2966, 2965, 2964, 2963, 2962, 2961, 2960, 2959, 2958, 2969,
			2968, 2967, 2966, 2965, 2964, 2963, 2962, 2961, 2960, 2959, 2958,
			2969, 2968, 2967, 2966, 2965, 2964, 2963, 2962, 2961, 2960, 2959,
			2958, 2969, 2968, 2967, 2966, 2964, 2963, 2962, 2961, 2960, 2959,
			2958, 2969, 2968, 2967, 2966, 2965, 2964, 2963, 2962, 2961, 2960,
			2959, 2958, 2969, 2968, 2967, 2966, 2965, 2964, 2963, 2962, 2961,
			2960, 2959, 2958, 2969, 2968, 2967, 2966, 2965, 2964, 2963, 2962,
			2961, 2960, 2959, 2958, /* 13 */2968, 2967, 2966, 2965, 2964, 2963,
			2967, 2966, 2965, 2964, 2966, 2965, 2967, 2966, 2965, 2964, 2968,
			2967, 2966, 2965, 2964, 2963, 2968, 2967, 2966, 2965, 2964, 2963,
			2967, 2966, 2965, 2964, 2968, 2967, 2966, 2965, 2964, 2963,

			/* 14 */			3076, 3074, 3076, 3075, 3074, 3077, 3076, 3075, 3074,
			3073, 3077, 3074, 3077, 3076, 3075, 3074, /* 15 */3204, 3204, 3203,
			3202, 3201, 3204, 3203, 3202, 3201, 3203, 3201, 3203, 3202, 3201,
			3204, 3203, 3201, 3204, /* 16 */3315, 3316, 3313, 3314, 3315, 3317,
			3318, 3314, 3317, 3314, 3315, 3316, 3317, 3313, 3314, 3315, 3316,
			3317, 3318, 3314, 3315, 3316, 3317, /* 17 */3319, 3320, 3323, 3318,
			3319, 3320, 3322, 3323, 3318, 3319, 3320, 3321, 3322, 3323, 3319,
			3320, 3321, 3322, 3319, 3320, 3322, 3323, 3318, 3319, 3320, 3323,
			3319, 3320, /* 18 */3315, 3316, 3312, 3313, 3314, 3315, 3316, 3312,
			3313, 3314, 3315, 3316, 3317, 3312, 3313, 3314, 3315, 3316, 3317,
			3318, 3312, 3313, 3314, 3316, 3317, 3312, 3313, 3314, 3316, 3317,
			3312, 3313, 3314, 3316, 3317, 3314, 3317, 3315, /* 19 */3314, 3315,
			3316, 3318, 3315, 3316, 3317, 3318, 3314, 3315, 3316, 3317, 3318,
			3314, 3315, 3316, 3314, 3315, /* 20 */3287, 3288, 3289, 3285, 3286,
			3287, 3288, 3289, 3290, 3287, 3288, 3289, 3290, 3287, 3288, 3289,
			3290, 3286, 3287, 3288, 3287, /* 21 */3229, 3232, 3228, 3229, 3230,
			3231, 3232, 3233, 3228, 3230, 3231, 3232, 3233, 3228, 3230, 3231,
			3232, 3232, /* 22 */3210, 3211, 3209, 3210, 3211, 3212, 3214, 3209,
			3211, 3212, 3213, 3214, 3209, 3211, 3212, 3213, 3209, 3210, 3211,
			3212, 3213, 3214, 3209, 3211, 3212, 3213, 3209, 3210, 3211, 3212,
			3213, 3209, 3211, 3213, /* 23 */3026, 3028, 3024, 3025, 3026, 3027,
			3028, 3029, 3025, 3026, 3027, 3028, 3029, 3030, 3024, 3025, 3028,
			3029, 3030, 3024, 3025, 3028, 3029, 3024, 3025, 3026, 3027, 3028,
			3029, 3028, 3029, 3030, 3025, 3026, 3027, 3028, 3029, /* 24 */3012,
			3013, 3014, 3015, 3016, 3012, 3015, 3016, 3012, 3015, 3016, 3011,
			3012, 3013, 3014, 3015, 3012, /* 25 */3012, 3014, 3012, 3013, 3014,
			3015, 3012, 3013, 3014, 3015, 3012, 3013, 3015, 3012, 3013, 3014,

			/* 26 */			3013, 3014, 3013, 3014, 3013, 3014, 3015, 3016, 3012,
			3013, 3014, 3015, 3016, 3017, 3012, 3013, 3014, 3015, 3011, 3012,
			3013, 3014, 3015, 3016, 3011, 3012, 3013, 3014, 3015, 3016, 3011,
			3016, 3011, 3013, 3014, 3015, 3016, 3013, 3014, 3016, /* 27 */3012,
			3014, 3012, 3013, 3014, 3015, 3016, 3012, 3015, 3012, 3013, 3014,
			3015, 3016, 3013, 3014, 3015, 3013, 3014, 3013, 3013, /* 28 */2946,
			2947, 2952, 2946, 2947, 2950, 2951, 2952, 2946, 2948, 2949, 2950,
			2951, 2946, 2948, 2949, 2950, 2951, 2946, 2947, 2948, 2949, 2950,
			2951, 2948, 2949, 2948, 2949, /* 29 */2955, 2958, 2959, 2954, 2955,
			2956, 2957, 2958, 2959, 2953, 2954, 2956, 2957, 2958, 2957, 2958,
			2959, /* 30 */3236, 3236, 3237, 3238, 3239, 3237, 3238, 3239, 3240,
			3236, 3237, 3238, 3239, 3240, 3236, 3237, 3238, 3239, 3237, 3238, /**/
			3245,
			3246, 3243, 3244, 3245, 3246, 3243, 3244, 3245, 3246, 3247, 3246,
			3247, 3261, 3260, 3261, 3262, 3260, 3261, 3263, 3260, 3263,
			3260, 3263, 3260, 3263, 3260, 3261, 3262, 3263, 3260, 3261, 3263,
			3234,
			3235, 3238, 3233, 3234, 3235, 3236, 3237, 3238, 3235, 3233, 3234,
			3235, 3236, 3233, 3234, 3235, 3236, 3237, 3238, 3290, 3291,
			3292, 3293, 3294, 3297, 3298, 3299, 3290, 3291, 3292, 3293, 3294,
			3295, 3296, 3297, 3298, 3299, 3290, 3291, 3292, 3293, 3294, 3295,
			3296, 3297, 3298, 3299, 3290, 3293, 3294, 3295, 3296, 3297, 3298,
			3299, 3290, 3293, 3294, 3295, 3296, 3297, 3298, 3299, 3290, 3291,
			3292, 3293, 3294, 3295, 3296, 3297, 3298, 3299, 3290, 3291, 3292,
			3293, 3294, 3295, 3296, 3297, 3298, 3299, 2662, 2663, 2661,
			2662, 2663, 2661, 2662, 2663, 2661, 2662, 2663, 2662, 2663, 2664,
			2665, 2666, 2665, 2666,/**/ 2664, 2665, 2666, 2664, 2665, 2666,
			2664, 2665, 2666, 2664, 2665, 2666, 2664, 2665, 2666, 2679,
			2680, 2679, 2680, 2676, 2677, 2678, 2679, 2680, 2676, 2677, 2678,
			2679, 2680, 2676, 2677, 2678, 2679, 2680, 2674, 2675, 2676, 2677,
			2678, 2679, 2680, 2675, 2676, 2677, 2678, 2679, 2680,  /**/2667,
			2668, 2669, 2670, 2671, 2667, 2668, 2669, 2670, 2671, 2667, 2668,
			2669, 2670, 2671, 2667, 2668, 2669, 2670, 2671, 2667, 2668, 2669,
			2670, 2671, 2667, 2668, 2669, 2670, 2671, 2667, 2668, 2669, 2670,
			2671, 2667, 2668, 2669, 2670, 2671,  /**/2681, 2682, 2683, 2684,
			2685, 2681, 2682, 2683, 2684, 2685, 2681, 2682, 2683, 2684, 2685,
			2681, 2682, 2683, 2684, 2685, 2681, 2682, 2683, 2684, 2685,  /**/
			2675, 2676, 2677, 2678, 2679, 2675, 2676, 2677, 2678, 2679, 2675,
			2676, 2677, 2678, 2679, 2676, 2677, 2678, 2679, 2677, 2678, 2679,  /**/
			2672,
			2673, 2674, 2675, 2672, 2673, 2674, 2675, 2672, 2673, 2674, 2675,
			2672, 2673, 2674, 2675, 2672, 2673, 2674, 2675, 2672, 2673, 2674,
			2675, 2672, 2673, 2674, 2675,  /**/2674, 2675, 2676, 2677, 2678,
			2674, 2675, 2676, 2677, 2678, 2674, 2675, 2676, 2677, 2678, 2674,
			2675, 2676, 2677, 2678, 2674, 2675, 2677, 2678,  /**/2685, 2686,
			2687, 2688, 2689, 2685, 2686, 2687, 2688, 2689, 2685, 2686, 2687,
			2688, 2689, 2685, 2686, 2687, 2688, 2689, 2685, 2686, 2687, 2688,
			2689,  /**/2668, 2669, 2670, 2671, 2672, 2668, 2669, 2670, 2671,
			2672, 2668, 2669, 2670, 2671, 2672, 2668, 2669, 2670, 2671, 2672,
			2668, 2669, 2670, 2671, 2672,  /**/2679, 2680, 2681, 2682, 2683,
			2679, 2680, 2681, 2682, 2683, 2679, 2680, 2681, 2682, 2683, 2679,
			2680, 2681, 2682, 2683, 2679, 2680, 2681, 2682, 2683,  /**/2673,
			2674, 2675, 2673, 2674, 2675, 2676, 2677, 2673, 2674, 2675, 2676,
			2677, 2673, 2674, 2675, 2676, 2677, 2673, 2674, 2675, 2676, 2677,  /**/
			2669,
			2670, 2671, 2672, 2669, 2670, 2671, 2672, 2673, 2669, 2670, 2671,
			2672, 2673, 2669, 2670, 2671, 2672, 2673, 2669, 2670, 2671, 2672,
			2673,  /**/2680, 2681, 2682, 2679, 2680, 2681, 2682, 2678, 2679,
			2680, 2681, 2682, 2678, 2679, 2680, 2681, 2682, 2678, 2679, 2680,
			2681, 2682,  /* 32 */3228, 3229, 3226, 3227, 3228, 3225, 3226, 3228,
			3229, 3226, 3227, 3228, 3229, 3230, 3225, 3226, 3227, 3228, 3229,
			3230, 3225, 3229, 3225, 3226, 3227, 3228, 3229, 3226,/**/ 3232,
			3233, 3234, 3235, 3236, 3237, 3232, 3233, 3234, 3235, 3236, 3231,
			3232, 3233, 3234, 3235, 3236, 3227, 3228, 3229, 3231, 3232, 3233,
			3234, 3235, 3236, 3225, 3226, 3227, 3228, 3229, 3230, 3231, 3233,
			3234, 3235, 3236, 3225, 3226, 3227, 3228, 3229, 3230, 3231, 3232,
			3233, 3234, 3235, 3236, 3225, 3228, 3229, 3230, 3231, 3232, 3235,
			3236, 3237, 3225, 3227, 3228, 3229, 3230, 3231, 3232, 3233, 3235,
			3236, 3237, 3225, 3227, 3228, 3229, 3230, 3231, 3232, 3233, 3235,
			3236, 3231, 3235,
		},
		{

			 /* 01 */		3404, 3404, 3403, 3403, 4302, 4302, 4302, 3401, 3401,
			3401, 3401, 3400, 3400, 3399, 3399, /* 02 */3398, 3398, 3398, 3398,
			3398, 3398, 3398, 3397, 3397, 3397, 3397, 3397, 3397, 3397, 3396,
			3396, 3396, 3396, 3396, 3396, 3395, 3395, 3395, 3395, 3395, 3395,
			3395, 3394, 3394, 3394, 3394, 3394, 3394, 3394, 3393, 3393, 3393,
			3393, 3393, 3393, 3393, 3392, 3392, 3392, 3392, 3392, 3392, 3391,
			3391, 3391, 3391, 3391, 3391, 3391, 3390, 3390, 3390, 3390, 3390,
			3390, 3390, 3389, 3389, 3389, 3389, 3389, 3389, 3389, 3388, 3388,
			3388, 3388, 3388, 3388, 3388, 3387, 3387, 3387, 3387, 3387, 3387,
			3387, 3386, 3386, 3386, 3386, 3386, 3386, 3386, /* 03 */3421, 3421,
			3421, 3421, 3421, 3421, 3422, 3422, 3423, 3423, 3423, 3423, 3423,
			3423, 3424, 3424, 3424, 3425, 3425, 3425, 3425, 3426, 3426, 3426,
			3427, 3427, /* 04 */3433, 3433, 3433, 3434, 3434, 3434, 3434, 3435,
			3435, 3435, 3436, 3436, 3437, 3437, 3437, 3437, 3437, 3437, 3438,
			3438, 3438, 3438, 3438, 3438, 3438, 3438, 3439, 3439, 3439, 3439,
			3439, 3439, 3439, 3440, 3440, 3440, 3440, 3440, 3440, 3440, 3441,
			3441, 3441, 3441, 3441, 3442, 3442, 3442, 3442, /* 05 */3411, 3411,
			3411, 3411, 3412, 3412, 3412, 3412, 3413, 3413, 3413, 3414, 3414,
			3414, 3414, 3415, 3415, 3415, 3415, 3415, 3415, 3416, 3416, 3416,
			3416, 3416, 3416, 3417, 3417, 3417, 3417, 3417, 3418, 3418, 3418,
			3418, 3418, 3419, 3419, /* 06 */3414, 3414, 3414, 3415, 3415, 3415,
			3415, 3416, 3416, 3416, 3416, 3416, 3417, 3417, 3417, 3417, 3417,
			3417, 3418, 3418, 3418, 3418, 3419, 3419, 3419, 3420, /* 07 */3495,
			3495, 3495, 3495, 3396, 3396, 3396, 3396, 3396, 3397, 3397, 3397,
			3397, 3397, 3398, 3398, 3398, 3398, 3398, 3398, 3398, 3399, 3399,
			3399, 3399, 3399, 3400, 3400, 3400, 3401, 3401, 3402, 3402, 3403,
			3404, /* 08 */3426, 3427, 3428, 3429, 3430, 3426, 3427, 3428, 3429,
			3430, 3426, 3427, 3428, 3429, 3430, 3426, 3427, 3428, 3429, 3430,
			3426, 3427, 3428, 3429, 3430, 3426, 3427, 3428, 3429, 3430, 3426,
			3427, 3428, 3429, 3430, 3227, 3228, 3229, 3426, 3427, 3430, 3420,
			3421, 3421, 3421, 3421, 3422, 3422, 3422, 3423, /* 09 */3385, 3385,
			3386, 3386, 3386, 3387, 3387, 3387, 3388, 3388, 3388, 3388, 3388,
			3388, 3388, 3389, 3389, 3389, 3389, 3389, 3390, 3390, 3390, 3390,
			3390, /* 10 */3383, 3383, 3383, 3383, 3383, 3383, 3383, 3384, 3384,
			3384, 3384, 3385, 3386, 3386, 3386, /* 11 */3385, 3385, 3385, 3385,
			3386, 3386, 3386, 3386, 3386, 3387, 3387, 3387, 3387, 3387, 3388,
			3388, 3388, 3388, 3388, 3389, 3390, 3390, /* 12 */3376, 3376, 3376,
			3376, 3376, 3376, 3376, 3376, 3376, 3376, 3376, 3377, 3377, 3377,
			3377, 3377, 3377, 3377, 3377, 3377, 3377, 3377, 3377, 3378, 3378,
			3378, 3378, 3378, 3378, 3378, 3378, 3378, 3378, 3378, 3378, 3379,
			3379, 3379, 3379, 3379, 3379, 3379, 3379, 3379, 3379, 3379, 3379,
			3380, 3380, 3380, 3380, 3380, 3380, 3380, 3380, 3380, 3380, 3380,
			3380, 3381, 3381, 3381, 3381, 3381, 3381, 3381, 3381, 3381, 3381,
			3381, 3382, 3382, 3382, 3382, 3382, 3382, 3382, 3382, 3382, 3382,
			3382, 3382, 3383, 3383, 3383, 3383, 3383, 3383, 3383, 3383, 3383,
			3383, 3383, 3383, 3384, 3384, 3384, 3384, 3384, 3384, 3384, 3384,
			3384, 3384, 3384, 3384, /* 13 */3391, 3391, 3391, 3391, 3391, 3391,
			3392, 3392, 3392, 3392, 3393, 3393, 3394, 3394, 3394, 3394, 3395,
			3395, 3395, 3395, 3395, 3395, 3396, 3396, 3396, 3396, 3396, 3396,
			3397, 3397, 3397, 3397, 3398, 3398, 3398, 3398, 3398, 3398,

			/* 14 */			3427, 3427, 3428, 3428, 3428, 3429, 3429, 3429, 3429,
			3429, 3430, 3430, 3431, 3431, 3431, 3431, /* 15 */3431, 3432, 3432,
			3432, 3432, 3433, 3433, 3433, 3433, 3434, 3434, 3435, 3435, 3435,
			3436, 3436, 3436, 3437, /* 16 */3160, 3160, 3161, 3161, 3161, 3161,
			3161, 3162, 3162, 3163, 3163, 3163, 3163, 3164, 3164, 3164, 3164,
			3164, 3164, 3165, 3165, 3165, 3165, /* 17 */3191, 3191, 3191, 3192,
			3192, 3192, 3192, 3192, 3193, 3193, 3193, 3193, 3193, 3193, 3194,
			3194, 3194, 3194, 3195, 3195, 3195, 3195, 3196, 3196, 3196, 3196,
			3197, 3197, /* 18 */3178, 3178, 3179, 3179, 3179, 3179, 3179, 3180,
			3180, 3180, 3180, 3180, 3180, 3181, 3181, 3181, 3181, 3181, 3181,
			3181, 3182, 3182, 3182, 3182, 3182, 3183, 3183, 3183, 3183, 3183,
			3184, 3184, 3184, 3184, 3184, 3185, 3185, 3186, /* 19 */3173, 3173,
			3173, 3173, 3174, 3174, 3174, 3174, 3175, 3175, 3175, 3175, 3175,
			3176, 3176, 3176, 3177, 3177, /* 20 */3187, 3187, 3187, 3188, 3188,
			3188, 3188, 3188, 3188, 3189, 3189, 3189, 3189, 3190, 3190, 3190,
			3190, 3191, 3191, 3191, 3192, /* 21 */3201, 3201, 3202, 3202, 3202,
			3202, 3202, 3202, 3203, 3203, 3203, 3203, 3203, 3204, 3204, 3204,
			3204, 3205, /* 22 */3243, 3243, 3244, 3244, 3244, 3244, 3244, 3245,
			3245, 3245, 3245, 3245, 3246, 3246, 3246, 3246, 3247, 3247, 3247,
			3247, 3247, 3247, 3248, 3248, 3248, 3248, 3249, 3249, 3249, 3249,
			3249, 3250, 3250, 3250, /* 23 */3245, 3245, 3246, 3246, 3246, 3246,
			3246, 3246, 3247, 3247, 3247, 3247, 3247, 3247, 3248, 3248, 3248,
			3248, 3248, 3249, 3249, 3249, 3249, 3250, 3250, 3250, 3250, 3250,
			3250, 3251, 3251, 3251, 3252, 3252, 3252, 3252, 3252, /* 24 */3257,
			3257, 3257, 3257, 3257, 3258, 3258, 3258, 3259, 3259, 3259, 3260,
			3260, 3260, 3260, 3260, 3261, /* 25 */3244, 3244, 3245, 3245, 3245,
			3245, 3246, 3246, 3246, 3246, 3247, 3247, 3247, 3248, 3248, 3248,

			/* 26 */			3220, 3220, 3221, 3221, 3222, 3222, 3222, 3222, 3223,
			3223, 3223, 3223, 3223, 3223, 3224, 3224, 3224, 3224, 3225, 3225,
			3225, 3225, 3225, 3225, 3226, 3226, 3226, 3226, 3226, 3226, 3227,
			3227, 3228, 3228, 3228, 3228, 3228, 3229, 3229, 3229, /* 27 */3203,
			3203, 3204, 3204, 3204, 3204, 3204, 3205, 3205, 3206, 3206, 3206,
			3206, 3206, 3207, 3207, 3207, 3208, 3208, 3209, 3210, /* 28 */3202,
			3202, 3202, 3203, 3203, 3203, 3203, 3203, 3204, 3204, 3204, 3204,
			3204, 3205, 3205, 3205, 3205, 3205, 3206, 3206, 3206, 3206, 3206,
			3206, 3207, 3207, 3208, 3208, /* 29 */3202, 3202, 3202, 3203, 3203,
			3203, 3203, 3203, 3203, 3204, 3204, 3204, 3204, 3204, 3205, 3205,
			3205, /* 30 */3403, 3404, 3404, 3404, 3404, 3405, 3405, 3405, 3405,
			3406, 3406, 3406, 3406, 3406, 3407, 3407, 3407, 3407, 3408, 3408, /**/
			3393,
			3393, 3394, 3394, 3394, 3394, 3395, 3395, 3395, 3395, 3395, 3396,
			3396,  /**/3396, 3397, 3397, 3397, 3398, 3398, 3398, 3399, 3399,
			3400, 3400, 3401, 3401, 3402, 3402, 3402, 3402, 3403, 3403, 3403,  /**/
			3382,
			3382, 3382, 3383, 3383, 3383, 3383, 3383, 3383, 3384, 3385, 3385,
			3385, 3385, 3386, 3386, 3386, 3386, 3386, 3386,  /**/3377, 3377,
			3377, 3377, 3377, 3377, 3377, 3377, 3378, 3378, 3378, 3378, 3378,
			3378, 3378, 3378, 3378, 3378, 3379, 3379, 3379, 3379, 3379, 3379,
			3379, 3379, 3379, 3379, 3380, 3380, 3380, 3380, 3380, 3380, 3380,
			3380, 3381, 3381, 3381, 3381, 3381, 3381, 3381, 3381, 3382, 3382,
			3382, 3382, 3382, 3382, 3382, 3382, 3382, 3382, 3383, 3383, 3383,
			3383, 3383, 3383, 3383, 3383, 3383, 3383,  /* 31 */3713, 3713, 3714,
			3714, 3714, 3715, 3715, 3715, 3716, 3716, 3716, 3717, 3717, 3718,
			3718, 3718, 3719, 3719,/**/ 3713, 3713, 3713, 3714, 3714, 3714,
			3715, 3715, 3715, 3716, 3716, 3716, 3717, 3717, 3717,  /**/3714,
			3714, 3715, 3715, 3716, 3716, 3716, 3716, 3716, 3717, 3717, 3717,
			3717, 3717, 3718, 3718, 3718, 3718, 3718, 3719, 3719, 3719, 3719,
			3719, 3719, 3719, 3720, 3720, 3720, 3720, 3720, 3720,  /**/3712,
			3712, 3712, 3712, 3712, 3713, 3713, 3713, 3713, 3713, 3714, 3714,
			3714, 3714, 3714, 3715, 3715, 3715, 3715, 3715, 3716, 3716, 3716,
			3716, 3716, 3717, 3717, 3717, 3717, 3717, 3718, 3718, 3718, 3718,
			3718, 3719, 3719, 3719, 3719, 3719,  /**/3714, 3714, 3714, 3714,
			3714, 3715, 3715, 3715, 3715, 3715, 3716, 3716, 3716, 3716, 3716,
			3717, 3717, 3717, 3717, 3717, 3718, 3718, 3718, 3718, 3718,  /**/
			3718, 3718, 3718, 3718, 3718, 3719, 3719, 3719, 3719, 3719, 3720,
			3720, 3720, 3720, 3720, 3721, 3721, 3721, 3721, 3722, 3722, 3722,  /**/
			3712,
			3712, 3712, 3712, 3713, 3713, 3713, 3713, 3714, 3714, 3714, 3714,
			3715, 3715, 3715, 3715, 3716, 3716, 3716, 3716, 3717, 3717, 3717,
			3717, 3718, 3718, 3718, 3718,  /**/3711, 3711, 3711, 3711, 3711,
			3712, 3712, 3712, 3712, 3712, 3713, 3713, 3713, 3713, 3713, 3714,
			3714, 3714, 3714, 3714, 3715, 3715, 3715, 3715, 3715,  /**/3722,
			3722, 3722, 3722, 3722, 3723, 3723, 3723, 3723, 3723, 3724, 3724,
			3724, 3724, 3724, 3725, 3725, 3725, 3725, 3725, 3726, 3726, 3726,
			3726, 3726,  /**/3725, 3725, 3725, 3725, 3725, 3726, 3726, 3726,
			3726, 3726, 3727, 3727, 3727, 3727, 3727, 3728, 3728, 3728, 3728,
			3728, 3729, 3729, 3729, 3729, 3729,  /**/3730, 3730, 3730, 3730,
			3730, 3731, 3731, 3731, 3731, 3731, 3732, 3732, 3732, 3732, 3732,
			3733, 3733, 3733, 3733, 3733, 3734, 3734, 3734, 3734, 3734,  /**/
			3727, 3727, 3727, 3728, 3728, 3728, 3728, 3728, 3729, 3729, 3729,
			3729, 3729, 3730, 3730, 3730, 3730, 3730, 3731, 3731, 3731, 3731,
			3731,  /**/3723, 3723, 3723, 3723, 3724, 3724, 3724, 3724, 3724,
			3725, 3725, 3725, 3725, 3725, 3726, 3726, 3726, 3726, 3726, 3727,
			3727, 3727, 3727, 3727,  /**/3726, 3726, 3726, 3727, 3727, 3727,
			3727, 3728, 3728, 3728, 3728, 3728, 3729, 3729, 3729, 3729, 3729,
			3730, 3730, 3730, 3730, 3730,  /* 32 */3287, 3287, 3288, 3288, 3288,
			3289, 3289, 3289, 3289, 3290, 3290, 3290, 3290, 3290, 3291, 3291,
			3291, 3291, 3291, 3291, 3292, 3292, 3293, 3293, 3293, 3293, 3293,
			3294,/**/ 3292, 3292, 3292, 3292, 3292, 3292, 3293, 3293, 3293,
			3293, 3293, 3294, 3294, 3294, 3294, 3294, 3294, 3295, 3295, 3295,
			3295, 3295, 3295, 3295, 3295, 3295, 3296, 3296, 3296, 3296, 3296,
			3296, 3296, 3296, 3296, 3296, 3296, 3297, 3297, 3297, 3297, 3297,
			3297, 3297, 3297, 3297, 3297, 3297, 3297, 3298, 3298, 3298, 3298,
			3298, 3298, 3298, 3298, 3298, 3299, 3299, 3299, 3299, 3299, 3299,
			3299, 3299, 3299, 3299, 3299, 3300, 3300, 3300, 3300, 3300, 3300,
			3300, 3300, 3300, 3300, 3301, 3301,
		},
	};

	 /*
	 WORLDMAP 2: (not-walk able places)
	 01 - Lumbridge cows
	 */
	public static int worldmap2[][] = {
		{
			/* 01 */		3257, 3258, 3260, 3261, 3261, 3262, 3261, 3262, 3257, 3258,
			3257, 3258, 3254, 3255, 3254, 3255, 3252, 3252, 3250, 3251, 3250,
			3251, 3249, 3250, 3249, 3250, 3242, 3242, 3243, 3243, 3257, 3244,
			3245, 3244, 3245, 3247, 3248, 3250, 3251, 3255, 3256, 3255, 3256,
			3259, 3260,
		},
		{
			/* 01 */		3256, 3256, 3256, 3256, 3266, 3266, 3267, 3267, 3270, 3270,
			3271, 3271, 3272, 3272, 3273, 3273, 3275, 3276, 3277, 3277, 3278,
			3278, 3279, 3279, 3280, 3280, 3285, 3286, 3289, 3290, 3289, 3297,
			3297, 3298, 3298, 3298, 3298, 3297, 3297, 3297, 3297, 3298, 3298,
			3297, 3297,
		},
	};

	public int remove = 2; // 1 = removes equipment, 2 = doesn't remove - xerozcheez
	public static int removeschaos[] = { 1, 2, 2, 2};

	public static int randomremoveschaos() {
		return removeschaos[(int) (Math.random() * removeschaos.length)];
	}

	public void gfxAll(int id, int Y, int X) {
		for (Player p : PlayerHandler.getInstance().getAllPlayer()) {
			if (p != null) {
				client person = (client) p;
				if ((person.playerName != null || person.playerName != "null")) {
					if (person.distanceToPoint(X, Y) <= 60) {
						person.stillgfx2(id, Y, X);
					}
				}
			}
		}
	}

	public int GetNPCBlockAnim(int id) {
		if (server.NpcAnimHandler.block[id] != 0) {
			return server.NpcAnimHandler.block[id];
		}
		return 0;
	}

	public boolean AttackPlayer(int NPCID) {
		int Player = npcs[NPCID].StartKilling;
		if (PlayerHandler.getInstance().getPlayer(Player) == null) {
			npcs[NPCID].ResetAttackPlayer();
			return false;
		}
		else if (PlayerHandler.getInstance().getPlayer(Player).DirectionCount < 2) {
			return false;
		}
		client plr = (client) PlayerHandler.getInstance().getPlayer(Player);
		int EnemyX = PlayerHandler.getInstance().getPlayer(Player).absX;
		int EnemyY = PlayerHandler.getInstance().getPlayer(Player).absY;
		//int arrowgfx;
		
		npcs[NPCID].enemyX = EnemyX;
		npcs[NPCID].enemyY = EnemyY;
		int EnemyHP = PlayerHandler.getInstance().getPlayer(Player).playerLevel[PlayerHandler.getInstance().getPlayer(Player).playerHitpoints];
		int EnemyMaxHP = getLevelForXP(
		PlayerHandler.getInstance().getPlayer(Player).playerXP[PlayerHandler.getInstance().getPlayer(Player).playerHitpoints]);
		boolean RingOfLife = false;
		if (PlayerHandler.getInstance().getPlayer(Player).playerEquipment[PlayerHandler.getInstance().getPlayer(Player).playerRing] == 2570) {
			RingOfLife = true;
		}
		
		if (PlayerHandler.getInstance().getPlayer(Player).attacknpc == NPCID || PlayerHandler.getInstance().getPlayer(Player).npcIndex == NPCID) {
			PlayerHandler.getInstance().getPlayer(Player).faceNPC = NPCID; // Xerozcheez: sets npc index for player to view
			PlayerHandler.getInstance().getPlayer(Player).faceNPCupdate = true; // Xerozcheez: updates face npc index so player faces npcs
			PlayerHandler.getInstance().getPlayer(Player).attacknpc = NPCID; // Xerozcheez: makes it so if player runs away the player attacks back when npc follows
			PlayerHandler.getInstance().getPlayer(Player).IsAttackingNPC = true; // Xerozcheez: makes it so if player runs away the player attacks back when npc follows
		}
		int hitDiff = 0;
		// FollowPlayerCB(NPCID, Player);
		if (HitPlayer(Player)) {
			if (npcs[NPCID].npcType == 2026) {
				hitDiff = 4 + misc.random(30); // Dharok
			}
			else if (npcs[NPCID].npcType == 2030) {
				hitDiff = 15 + misc.random(15); // Verac
			}
			else if (npcs[NPCID].npcType == 2029) {
				hitDiff = 0 + misc.random(30); // Torag
			}
			else if (npcs[NPCID].npcType == 2027) {
				hitDiff = 15 + misc.random(20); // Guthan
			}
			else {
				hitDiff = misc.random(npcs[NPCID].MaxHit);
			}
		}
		else {
			hitDiff = 0;
		}
		if (prayerhandler.pmelee[plr.playerId]) {
			hitDiff = misc.random(npcs[NPCID].MaxHit) / 3;
		}
		if (GoodDistance(npcs[NPCID].absX, npcs[NPCID].absY, EnemyX, EnemyY, 1) == true || npcs[NPCID].npcType == 3200 || npcs[NPCID].npcType == 2745 || npcs[NPCID].npcType == 2028) {
			if (npcs[NPCID].actionTimer == 0) {
				if (RingOfLife == true && EnemyHP <= (int) ((double) ((double) EnemyMaxHP / 10.0) + 0.5)) {
					PlayerHandler.getInstance().getPlayer(Player).SafeMyLife = true;
				}
				else {
					if (PlayerHandler.getInstance().getPlayer(Player).IsDead == true) {
						npcs[NPCID].ResetAttackPlayer();
					}
					else {
						if (server.NpcAnimHandler.atk[npcs[NPCID].npcType] != 0) {
							npcs[NPCID].animNumber = server.NpcAnimHandler.atk[npcs[NPCID].npcType];
						}
						else {
							npcs[NPCID].animNumber = server.NpcAnimHandler.atk[npcs[NPCID].npcType];
						}
						npcs[NPCID].TurnNPCTo(EnemyX, EnemyY);
						npcs[NPCID].FocusUpdateRequired = true;
						npcs[NPCID].animUpdateRequired = true;
						npcs[NPCID].updateRequired = true;
						if ((EnemyHP - hitDiff) < 0) {
							hitDiff = EnemyHP;
						}
						plr.startAnimation(plr.GetBlockAnim(plr.playerEquipment[plr.playerShield]));
						PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
						PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
						PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
						PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
						npcs[NPCID].actionTimer = 6;
					}
				}
				return true;
			}
		}
		return false;
	}

	public int EnemyIndexP = -1;

	public boolean AttackPlayerMage(int NPCID) {
		int Player = npcs[NPCID].StartKilling;
		client p = (client) PlayerHandler.getInstance().getPlayer(Player);
		if (PlayerHandler.getInstance().getPlayer(Player) == null) {
			npcs[NPCID].ResetAttackPlayer();
			return false;
		}
		else if (PlayerHandler.getInstance().getPlayer(Player).DirectionCount < 2) {
			return false;
		}
		int MageEmote = 711;
		int EnemyX = PlayerHandler.getInstance().getPlayer(Player).absX;
		int EnemyY = PlayerHandler.getInstance().getPlayer(Player).absY;
		int EnemyHP = PlayerHandler.getInstance().getPlayer(Player).playerLevel[PlayerHandler.getInstance().getPlayer(Player).playerHitpoints];
		int EnemyMaxHP = getLevelForXP(
		PlayerHandler.getInstance().getPlayer(Player).playerXP[PlayerHandler.getInstance().getPlayer(Player).playerHitpoints]);
		int casterX = npcs[NPCID].absX;
		int casterY = npcs[NPCID].absY;
		int offsetY = (casterX - EnemyX) * -1;
		int offsetX = (casterY - EnemyY) * -1;
		//int MagehitDiff = 0;
		boolean RingOfLife = false;

		if (PlayerHandler.getInstance().getPlayer(Player).playerEquipment[PlayerHandler.getInstance().getPlayer(Player).playerRing] == 2570) {
			RingOfLife = true;
		}
		int hitDiff = 0;
		int damage = misc.random(hitDiff) / 3;
		if (prayerhandler.pmage[p.playerId]) {
			hitDiff = damage;
		}
		if (npcs[NPCID].actionTimer == 0) {
			if (RingOfLife == true && EnemyHP <= (int) ((double) ((double) EnemyMaxHP / 10.0) + 0.5)) {
				PlayerHandler.getInstance().getPlayer(Player).SafeMyLife = true;
			}
			else {
				if (PlayerHandler.getInstance().getPlayer(Player).IsDead == true) {
					npcs[NPCID].ResetAttackPlayer();
				}
				else {
					if (npcs[NPCID].npcType == 277) { // Fire Warrior
						hitDiff = misc.random(20);
						PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
						PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
						PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
						PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
						p.stillgfx(78, p.absY, p.absX);
						MageEmote = 811; // godly spell
					}
					if (npcs[NPCID].npcType == 158) { // Shadow Warrior
						if (HitPlayerMage(Player)) {
							p.playerGfx(382, 1);
							hitDiff = misc.random(25);
							PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
							PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
							MageEmote = 1979; // barrages
						}
						else {
							p.gfx110(339);
							MageEmote = 1979; // barrages
						}
					} 
					if (npcs[NPCID].npcType == 2060) { // Shadow Warrior
						p.attackPlayersWithin(437, 20, 15);
						MageEmote = 2102; // barrages
						p.stillgfx(437, p.absY, p.absX);
						hitDiff = misc.random(20);
						PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
						PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
						PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
						PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
					} 
					if (npcs[NPCID].npcType == 1592) {
						GraphicsHandler.createProjectile(438, npcs[NPCID].absY, npcs[NPCID].absX, offsetY, offsetX, 50, 75, 438, 30, 30, EnemyIndexP + 1, 410, EnemyY, EnemyX, 0);
						if (PlayerHandler.getInstance().getPlayer(Player).playerEquipment[PlayerHandler.getInstance().getPlayer(Player).playerShield] == 1540 && prayerhandler.pmage[p.playerId]) {
							MageEmote = 81; // barrages
							hitDiff = misc.random(3);  
							PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
							PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;		   
							p.sendMessage("Your sheild blocks all of the dragon's breath.");
						}
						else if (PlayerHandler.getInstance().getPlayer(Player).playerEquipment[PlayerHandler.getInstance().getPlayer(Player).playerShield] == 1540) {
							hitDiff = misc.random(10);
							PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
							PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
							MageEmote = 81; // barrages
							p.sendMessage("Your sheild blocks most of the dragon's breath.");
						}
						else {
							hitDiff = misc.random(50);
							PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
							PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
							MageEmote = 81; // barrages
						}
					}
					if (npcs[NPCID].npcType == 509) { // Nazastarool
						p.stillgfx(390, p.absY, p.absX);
						hitDiff = misc.random(30);
						MageEmote = 1979; // barrages
						PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
						PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
						PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
						PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
					}
					if (npcs[NPCID].npcType == 2745) {
						if (jadMage == 0) {
							npcs[NPCID].animNumber = 2652; // range
							hitDiff = 6 + misc.random(18);
							p.stillgfx(451, p.absY, p.absX);
							jadMage = 1;
							PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
							PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
							MageEmote = 2656;
						}
						else {
							if (jadMage == 1) {
								npcs[NPCID].animNumber = 2655; // melee
								hitDiff = 2 + misc.random(12);
								jadMage = 2;
								PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
								PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
								PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
								PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
								MageEmote = 2655;
							}
							else {
								if (jadMage == 2) {
									npcs[NPCID].animNumber = 2656; // mage
									p.stillgfx(437, p.absY, p.absX);
									hitDiff = 8 + misc.random(15);
									PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
									PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
									PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
									PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
									jadMage = 0;
									MageEmote = 2652;
								}
							}
						}
					}
					if (npcs[NPCID].npcType == 2025) { // Ahrim
						if (HitPlayerMage(Player)) {
							p.playerGfx(369, 1);
							hitDiff = misc.random(25);
							PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
							PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
							MageEmote = 1979; // barrages
						}
						else {
							p.gfx110(339);
							MageEmote = 1979; // barrages
						}
					}
					if (npcs[NPCID].npcType == 912) {
						if (HitPlayerMage(Player)) {
							p.playerGfx(78, 1);
							hitDiff = misc.random(10);
							MageEmote = 811; // barrages
							PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
							PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
						}
						else {
							hitDiff = 0;
							p.gfx110(339);
							MageEmote = 811; // barrages
						}
					}
					if (npcs[NPCID].npcType == 913) {			  
						if (HitPlayerMage(Player)) {
							p.gfx110(76);
							hitDiff = misc.random(10);
							MageEmote = 811; // barrages
							PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
							PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
						}
						else {
							p.gfx110(339);
							MageEmote = 811; // barrages
						}
					}
					if (npcs[NPCID].npcType == 914) {
						if (HitPlayerMage(Player)) {
							p.gfx110(77);
							hitDiff = misc.random(10);
							PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
							PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
							MageEmote = 811; // barrages
						}
						else {
							p.gfx110(339);
							MageEmote = 811; // barrages
						}
					}
					if (npcs[NPCID].npcType == 1643) {
						if (HitPlayerMage(Player)) {
							p.playerGfx(369, 1);
							hitDiff = misc.random(10);
							PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
							PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
							MageEmote = 1979; // barrages
						}
						else {
							p.gfx110(339);
							MageEmote = 1979; // barrages
						}
					}
					if (npcs[NPCID].npcType == 3200) {
						GraphicsHandler.createProjectile(410, npcs[NPCID].absY, npcs[NPCID].absX, offsetY, offsetX, 50, 75, 396, 30, 30, EnemyIndexP + 1, 410, EnemyY, EnemyX, 0);
						hitDiff = misc.random(20);
						PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
						PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
						PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
						PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
						MageEmote = 3146; // barrages
					}
					if (npcs[NPCID].npcType == 2361 && GoodDistance(npcs[NPCID].absX, npcs[NPCID].absY, EnemyX, EnemyY, 30) == true) { // Elf warrior
						MageEmote = 426;
						hitDiff = misc.random(30 / 2);
						PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
						PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
						PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
						PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
						GraphicsHandler.createSpell(410, npcs[NPCID].absY, npcs[NPCID].absX, offsetY, offsetX, 50, 95, 249, 43, 31, EnemyIndexP + 1, 410, EnemyY, EnemyX);
						npcs[NPCID].gfx110(250);
					}
					if (npcs[NPCID].npcType == 50) {
						GraphicsHandler.createProjectile(410, npcs[NPCID].absY, npcs[NPCID].absX, offsetY, offsetX, 50, 75, 398, 30, 30, EnemyIndexP + 1, 410, EnemyY, EnemyX, 0);
						hitDiff = misc.random(10);
						PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
						PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
						PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
						PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
						MageEmote = 81; // barrages
					}
					if (npcs[NPCID].npcType == 53) {
						GraphicsHandler.createProjectile(438, npcs[NPCID].absY, npcs[NPCID].absX, offsetY, offsetX, 50, 75, 438, 30, 30, EnemyIndexP + 1, 410, EnemyY, EnemyX, 0);
						if (PlayerHandler.getInstance().getPlayer(Player).playerEquipment[PlayerHandler.getInstance().getPlayer(Player).playerShield] == 1540) {
							hitDiff = misc.random(10);
							MageEmote = 81; // barrages
							PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
							PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
						}
						else {
							hitDiff = misc.random(30);
							MageEmote = 81; // barrages
							PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
							PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
							PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
						}
					}
					if (npcs[NPCID].npcType == 2028) { // karil
						GraphicsHandler.createProjectile(410, npcs[NPCID].absY, npcs[NPCID].absX, offsetY, offsetX, 50, 75, 27, 30, 30, EnemyIndexP + 1, 410, EnemyY, EnemyX, 0);
						hitDiff = 1 + misc.random(6);
						MageEmote = 2075; // Karil's shooting emote
						p.startAnimation(p.GetBlockAnim(p.playerEquipment[p.playerShield]));
						PlayerHandler.getInstance().getPlayer(Player).hitDiff = hitDiff;
						PlayerHandler.getInstance().getPlayer(Player).updateRequired = true;
						PlayerHandler.getInstance().getPlayer(Player).hitUpdateRequired = true;
						PlayerHandler.getInstance().getPlayer(Player).appearanceUpdateRequired = true;
					}
					npcs[NPCID].animNumber = MageEmote; // Casting Emote
					npcs[NPCID].animUpdateRequired = true;
					npcs[NPCID].updateRequired = true;
					if ((EnemyHP - hitDiff) < 0) {
						hitDiff = EnemyHP;
					}
					npcs[NPCID].actionTimer = 6;
				}
			}
			return true;
		}
		return false;
	}

	public boolean AttackNPCMage(int NPCID) {
		int EnemyX = npcs[npcs[NPCID].attacknpc].absX;
		int EnemyY = npcs[npcs[NPCID].attacknpc].absY;
		int EnemyHP = npcs[npcs[NPCID].attacknpc].HP;
		int hitDiff = 0;
		//int Npchitdiff = 0;
		//int wepdelay = 0;
		int Player = npcs[NPCID].StartKilling;
		client p = (client) PlayerHandler.getInstance().getPlayer(Player);
		// hitDiff = misc.random(npcs[NPCID].MaxHit);
		if (npcs[NPCID].actionTimer == 0) {
			if (npcs[npcs[NPCID].attacknpc].IsDead == true) {
				ResetAttackNPC(NPCID);
				// npcs[NPCID].textUpdate = "Oh yeah I win bitch!";
				// npcs[NPCID].textUpdateRequired = true;
				npcs[NPCID].animNumber = 2103;
				npcs[NPCID].animUpdateRequired = true;
				npcs[NPCID].updateRequired = true;
			}
			else {
				npcs[NPCID].animNumber = 711; // mage attack
				if (npcs[NPCID].npcType == 1645) {
					p.playerGfx(369, 1);
					hitDiff = 6 + misc.random(43);
				}
				if (npcs[NPCID].npcType == 1645) {
					p.playerGfx(369, 1);
					hitDiff = 6 + misc.random(43);
				}
				if (npcs[NPCID].npcType == 509) {
					gfxAll(365, EnemyY, EnemyX);
					hitDiff = 8 + misc.random(51); 
				}
				if (npcs[NPCID].npcType == 1241) {
					gfxAll(363, EnemyY, EnemyX);
					hitDiff = 2 + misc.random(19); 
				}
				if (npcs[NPCID].npcType == 1246) {
					gfxAll(368, npcs[NPCID].absY, npcs[NPCID].absX);
					gfxAll(367, EnemyY, EnemyX);
					hitDiff = 4 + misc.random(35); 
				}
								
				npcs[NPCID].animUpdateRequired = true;
				npcs[NPCID].updateRequired = true;
				if (EnemyHP - hitDiff < 0)
					hitDiff = EnemyHP;
				npcs[npcs[NPCID].attacknpc].hitDiff = hitDiff;
				npcs[npcs[NPCID].attacknpc].attacknpc = NPCID;
				npcs[npcs[NPCID].attacknpc].updateRequired = true;
				npcs[npcs[NPCID].attacknpc].hitUpdateRequired = true;
				npcs[NPCID].actionTimer = 6;
				return true;
			}
			return false;
		}
		return false;
	}

	public boolean AttackNPC(int NPCID) {
		if (npcs[npcs[NPCID].attacknpc] != null) {
			int EnemyX = npcs[npcs[NPCID].attacknpc].absX;
			int EnemyY = npcs[npcs[NPCID].attacknpc].absY;
			int EnemyHP = npcs[npcs[NPCID].attacknpc].HP;
			int hitDiff = 0;
			//int Npchitdiff = 0;
			//int wepdelay = 0;
			hitDiff = misc.random(npcs[NPCID].MaxHit);
			if (GoodDistance(EnemyX, EnemyY, npcs[NPCID].absX, npcs[NPCID].absY, 1) == true) {
				if (npcs[npcs[NPCID].attacknpc].IsDead == true) {
					ResetAttackNPC(NPCID);
					// npcs[NPCID].textUpdate = "Oh yeah I win bitch!";
					// npcs[NPCID].textUpdateRequired = true;
					npcs[NPCID].animNumber = 2103;
					npcs[NPCID].animUpdateRequired = true;
					npcs[NPCID].updateRequired = true;
				}
				else {
					if (EnemyHP - hitDiff < 0)
						hitDiff = EnemyHP;
					if (npcs[NPCID].npcType == 9)
						npcs[NPCID].animNumber = 386;
					if (npcs[NPCID].npcType == 3200)
						npcs[NPCID].animNumber = 0x326;
					// drags: chaos ele emote ( YESSS ) 
					if (npcs[NPCID].npcType == 1605)
						npcs[NPCID].animNumber = 386; // drags: abberant spector death ( YAY )
					npcs[NPCID].animUpdateRequired = true;
					npcs[NPCID].updateRequired = true;
					npcs[npcs[NPCID].attacknpc].hitDiff = hitDiff;
					npcs[npcs[NPCID].attacknpc].attacknpc = NPCID;
					npcs[npcs[NPCID].attacknpc].updateRequired = true;
					npcs[npcs[NPCID].attacknpc].hitUpdateRequired = true;
					npcs[NPCID].actionTimer = 6;
					return true;
				}
			}
		}
		return false;
	}

	public boolean ResetAttackNPC(int NPCID) {
		npcs[NPCID].IsUnderAttackNpc = false;
		npcs[NPCID].IsAttackingNPC = false;
		npcs[NPCID].attacknpc = -1;
		npcs[NPCID].RandomWalk = true;
		npcs[NPCID].animUpdateRequired = true;
		npcs[NPCID].updateRequired = true;
		return true;
	}

	public int getLevelForXP(int exp) {
		int points = 0;
		int output = 0;
		for (int lvl = 1; lvl <= 135; lvl++) {
			points += Math.floor((double) lvl + 300.0 * Math.pow(2.0, (double) lvl / 7.0));
			output = (int) Math.floor(points / 4);
			if (output >= exp) {
				return lvl;
			}
		}
		return 0;
	}

	public boolean GoodDistance(int objectX, int objectY, int playerX, int playerY, int distance) {
		for (int i = 0; i <= distance; i++) {
			for (int j = 0; j <= distance; j++) {
				if ((objectX + i) == playerX && ((objectY + j) == playerY || (objectY - j) == playerY || objectY == playerY)) {
					return true;
				}
				else if ((objectX - i) == playerX && ((objectY + j) == playerY || (objectY - j) == playerY || objectY == playerY)) {
					return true;
				}
				else if (objectX == playerX && ((objectY + j) == playerY || (objectY - j) == playerY || objectY == playerY)) {
					return true;
				}
			}
		}
		return false;
	}

	public boolean loadAutoSpawn() {
		try {
			int nb = 0;
			ResultSet rs = DBService.getInstance().doQuery("SELECT * FROM npc_spawn");
			while (rs.next()) {
				newNPC(rs.getInt("npcid"), rs.getInt("coordx"), rs.getInt("coordy"), rs.getInt("height"), rs.getInt("rangex1"), rs.getInt("rangey1"), rs.getInt("rangex2"), rs.getInt("rangey2"), rs.getInt("walktype"), GetNpcListHP(rs.getInt("npcid")), true);
				nb++;
			}
			LogService.getInstance().logInfo("npc_spawn load ("+nb+")");
			return true;
		}
		catch (SQLException ex) {
			LogService.getInstance().logError("Can't load npc_spawn");
			LogService.getInstance().logError(ex.getMessage());
			return false;
		}
	}

	public int GetNpcListHP(int NpcID) {
		for (int i = 0; i < maxListedNPCs; i++) {
			if (NpcList[i] != null) {
				if (NpcList[i].npcId == NpcID) {
					return NpcList[i].npcHealth;
				}
			}
		}
		return 0;
	}

	public boolean loadNPCList() {
		try {
			int nb = 0;
			
			ResultSet rs = DBService.getInstance().doQuery("SELECT * FROM npc_template");
			while (rs.next()) {
				newNPCList(rs.getInt("id"), rs.getString("name"), rs.getInt("combat"), rs.getInt("health"));
				nb++;
			}
			LogService.getInstance().logInfo("npc_template load ("+nb+")");
			return true;
		}
		catch (SQLException ex) {
			LogService.getInstance().logError("Can't load npc_template");
			LogService.getInstance().logError(ex.getMessage());
			return false;
		}
	}
	
	public void loadNPCDrops() {
		try {
			int nb = 0;
			ResultSet rs = DBService.getInstance().doQuery("SELECT * FROM npc_drops");
			while (rs.next()) {
				npcdrop.addDrop(rs.getInt("npcid"), rs.getInt("item"), rs.getInt("nb"), rs.getInt("hopedown"));
				nb++;
			}
			LogService.getInstance().logInfo("npc_drops load ("+nb+")");
		}
		catch (SQLException ex) {
			LogService.getInstance().logError("Can't load npc_drops");
			LogService.getInstance().logError(ex.getMessage());
		}
	}

	public void println(String str) {
		System.out.println(str);
	}
}
